package servlet;

import listener.a;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import  java.util.Properties;



public class DisplayServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//      接受修改后的值
        String name_change = req.getParameter("name_change");
        String password_change = req.getParameter("password_change");
//      输出修改后的密码
        PrintWriter out = resp.getWriter();
        out.write("修改后的名字是"+name_change+"<br>");
        out.write("修改后的密码是"+password_change+"<br>");

//       接受全局变量信息
        ServletContext servletContext = this.getServletContext();
        String username = servletContext.getInitParameter("username");
        out.write("全局信息是"+username+"<br>");
//       接受局部信息
        ServletConfig servletConfig = this.getServletConfig();
        String username1 = servletConfig.getInitParameter("username");
        out.write("局部信息是"+username1+"<br>");

//        接受db.properties的值
//        新建
        Properties properties = new Properties();
        a a = new a();
        a.test();

//        设置路径
        BufferedReader file_load = new BufferedReader(new FileReader(a.filePath));
//        添加路径
        properties.load(file_load);
//        读取名字密码
        String name = properties.getProperty("name");
        String password = properties.getProperty("password");
//        输出
        out.write("读取到的用户名字是"+name+"<br>");
        out.write("读取到的用户密码是"+password+"<br>");

        //获取Cookie 值
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies){
            out.println("cookie name:"+ URLDecoder.decode(cookie.getName(),"UTF-8")+"<br>"+"cookie value:"+cookie.getValue()+"<br>");
        }


    }




}
